<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>校园智能助手系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #e9f7ef;
            border-radius: 5px;
            display: none;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>校园智能助手系统</h1>
        
        <div id="register-section">
            <h2>用户注册</h2>
            <form id="register-form">
                <div class="form-group">
                    <label for="reg-username">用户名:</label>
                    <input type="text" id="reg-username" required>
                </div>
                <div class="form-group">
                    <label for="reg-password">密码:</label>
                    <input type="password" id="reg-password" required>
                </div>
                <div class="form-group">
                    <label for="reg-role">角色:</label>
                    <select id="reg-role" required>
                        <option value="student">学生</option>
                        <option value="teacher">教师</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="reg-phone">手机号:</label>
                    <input type="text" id="reg-phone">
                </div>
                <div class="form-group">
                    <label for="reg-email">邮箱:</label>
                    <input type="email" id="reg-email">
                </div>
                <button type="submit">注册</button>
            </form>
        </div>
        
        <div id="login-section">
            <h2>用户登录</h2>
            <form id="login-form">
                <div class="form-group">
                    <label for="login-username">用户名:</label>
                    <input type="text" id="login-username" required>
                </div>
                <div class="form-group">
                    <label for="login-password">密码:</label>
                    <input type="password" id="login-password" required>
                </div>
                <button type="submit">登录</button>
            </form>
        </div>
        
        <div id="result" class="result"></div>
    </div>

    <script>
        // 注册表单提交
        document.getElementById('register-form').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const user = {
                username: document.getElementById('reg-username').value,
                password: document.getElementById('reg-password').value,
                role: document.getElementById('reg-role').value,
                phone: document.getElementById('reg-phone').value,
                email: document.getElementById('reg-email').value
            };
            
            fetch('/api/users/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(user)
            })
            .then(response => response.text())
            .then(data => {
                const resultDiv = document.getElementById('result');
                resultDiv.style.display = 'block';
                try {
                    const userData = JSON.parse(data);
                    if (userData.id) {
                        resultDiv.classList.remove('error');
                        resultDiv.innerHTML = `<h3>注册成功!</h3><p>用户ID: ${userData.id}</p><p>用户名: ${userData.username}</p>`;
                    } else {
                        resultDiv.classList.add('error');
                        resultDiv.innerHTML = `<h3>注册失败</h3><p>${data}</p>`;
                    }
                } catch (e) {
                    resultDiv.classList.add('error');
                    resultDiv.innerHTML = `<h3>注册失败</h3><p>${data}</p>`;
                }
            })
            .catch(error => {
                const resultDiv = document.getElementById('result');
                resultDiv.style.display = 'block';
                resultDiv.classList.add('error');
                resultDiv.innerHTML = `<h3>注册失败</h3><p>${error.message}</p>`;
            });
        });
        
        // 登录表单提交
        document.getElementById('login-form').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const user = {
                username: document.getElementById('login-username').value,
                password: document.getElementById('login-password').value
            };
            
            fetch('/api/users/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(user)
            })
            .then(response => response.text())
            .then(data => {
                const resultDiv = document.getElementById('result');
                resultDiv.style.display = 'block';
                try {
                    const userData = JSON.parse(data);
                    if (userData.id) {
                        resultDiv.classList.remove('error');
                        resultDiv.innerHTML = `<h3>登录成功!</h3><p>欢迎, ${userData.username} (${userData.role})</p>`;
                    } else {
                        resultDiv.classList.add('error');
                        resultDiv.innerHTML = `<h3>登录失败</h3><p>${data}</p>`;
                    }
                } catch (e) {
                    resultDiv.classList.add('error');
                    resultDiv.innerHTML = `<h3>登录失败</h3><p>${data}</p>`;
                }
            })
            .catch(error => {
                const resultDiv = document.getElementById('result');
                resultDiv.style.display = 'block';
                resultDiv.classList.add('error');
                resultDiv.innerHTML = `<h3>登录失败</h3><p>${error.message}</p>`;
            });
        });
    </script>
</body>
</html>